package com.cheer.ba.view.activity;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.DownloadListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.cheer.ba.R;
import com.cheer.ba.constant.H5Address;
import com.cheer.ba.constant.IntentKey;
import com.cheer.ba.utils.LogUtils;
import com.cheer.ba.utils.ShareNewsUtil;
import com.cheer.ba.utils.StringUtils;
import com.cheer.ba.view.base.BaseCommAty;
import com.cheer.ba.widget.ProgressWebView;
import com.umeng.socialize.UMShareAPI;

import butterknife.BindView;

/**
 * 功能：显示H5的界面,带标题
 * 说明：
 */
public class CommH5WithTitleAty extends BaseCommAty {

    //进度条
    @BindView(R.id.id_webview)
    ProgressWebView webView;

    //错误url
    @BindView(R.id.id_error_ulr_txt)
    TextView mTxtErrorUrl;

    //路径地址
    private String url;

    //标题名
    private String titleName;

    //是否支持缩放 true 支持
    private boolean isSupportZoom;

    private ShareNewsUtil mShare;
    private boolean isShare;
    private String mTitle;
    private String mLitTitle;
    private String mShareUrl;

    @Override
    protected boolean isSupportSwipeback() {
        return false;
    }

    /**
     * 启动h5界面，带标题
     */
    public static void startCommonH5(Activity activity, String url, String titleName, boolean isSupportZoom) {
        Intent intent = new Intent(activity, CommH5WithTitleAty.class);
        intent.putExtra(IntentKey.General.KEY_URL, url);
        intent.putExtra(IntentKey.General.KEY_DES, titleName);
        intent.putExtra(IntentKey.General.KEY_BOOLEAN, isSupportZoom);
        activity.startActivity(intent);
    }

    /**
     * 启动h5界面，带标题
     */
    public static void startCommonH5(Activity activity, String url, String titleName, boolean isSupportZoom, boolean isShare, String title, String litTitle, String shareUrl) {
        Intent intent = new Intent(activity, CommH5WithTitleAty.class);
        intent.putExtra(IntentKey.General.KEY_URL, url);
        intent.putExtra(IntentKey.General.KEY_DES, titleName);
        intent.putExtra(IntentKey.General.KEY_BOOLEAN, isSupportZoom);
        intent.putExtra(IntentKey.General.KEY_SHARE, isShare);
        intent.putExtra(IntentKey.General.KEY_TITLE, title);
        intent.putExtra(IntentKey.General.KEY_DATA, litTitle);
        intent.putExtra(IntentKey.General.KEY_SHARE_URL, shareUrl);
        activity.startActivity(intent);
    }

    @Nullable
    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.aty_comm_h5, null, false));
    }

    @Override
    public void initView() {
        url = getIntent().getStringExtra(IntentKey.General.KEY_URL);
        titleName = getIntent().getStringExtra(IntentKey.General.KEY_DES);
        isSupportZoom = getIntent().getBooleanExtra(IntentKey.General.KEY_BOOLEAN, false);
        isShare = getIntent().getBooleanExtra(IntentKey.General.KEY_SHARE, false);
        if (isShare) {
            mTitle = getIntent().getStringExtra(IntentKey.General.KEY_TITLE);
            mLitTitle = getIntent().getStringExtra(IntentKey.General.KEY_DATA);
            mShareUrl = getIntent().getStringExtra(IntentKey.General.KEY_SHARE_URL);
            setTitleIcon(R.drawable.ic_sharer, false, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mShare = new ShareNewsUtil(aty, mTitle, mLitTitle, mShareUrl, "");
                    mShare.showShare();
                }
            });
        }
        if (!StringUtils.isEmpty(titleName)) {
            setTitleText(titleName);
        }

        webView.setVisibility(View.VISIBLE);
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setUseWideViewPort(true);
        if (isSupportZoom) {
            //支持屏幕缩放
            settings.setSupportZoom(true);
            settings.setBuiltInZoomControls(true);
            //不显示webview缩放按钮
            settings.setDisplayZoomControls(false);
        }
        settings.setLoadWithOverviewMode(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webView.setVerticalScrollBarEnabled(false);
        webView.setVerticalScrollbarOverlay(false);
        webView.setHorizontalScrollBarEnabled(false);
        webView.setHorizontalScrollbarOverlay(false);
        webView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        webView.setDownloadListener(new DownloadListener() {
            @Override
            public void onDownloadStart(String url, String userAgent,
                                        String contentDisposition, String mimetype,
                                        long contentLength) {
                if (url != null && url.startsWith("http://"))
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri
                            .parse(url)));
            }
        });
        if (!StringUtils.isEmpty(url) && (url.startsWith("http://") || url.startsWith("https://"))) {
            webView.loadUrl(url);
        } else {
            mTxtErrorUrl.setVisibility(View.VISIBLE);
            webView.setVisibility(View.GONE);
            if (StringUtils.isEmpty(url)) {
                mTxtErrorUrl.setText("跳转链接为空");
            } else {
                mTxtErrorUrl.setText(url);
            }
        }
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.startsWith("tel:")) {
                    LogUtils.LogD("Tel", "---------========" + url);
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    startActivity(intent);
                    return true;
                }
                view.loadUrl(url);
                return true;
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /** attention to this below ,must add this**/
        UMShareAPI.get(aty).onActivityResult(requestCode, resultCode, data);
    }
}
